<template>
  <div>
    <div class="forgot-panel">
      <el-form class="forgot-form" ref="form" :model="form" :rules="formRules" label-width="0px">
        <el-form-item class="no-bottom" prop="password">
          <el-input type="password" v-model="form.password" :placeholder="$root.lang.forgotStep3.password"></el-input>
          <p class="form-item-tip">{{ $root.lang.forgotStep3.passwordTip }}</p>
        </el-form-item>
        <el-form-item class="no-bottom" prop="confirm">
          <el-input type="password" v-model="form.confirm" @keyup.enter.native="onSubmit" :placeholder="$root.lang.forgotStep3.confirm"></el-input>
        </el-form-item>
        <el-form-item>
          <p class="form-error">{{ errmsg }}</p>
          <el-button class="forgot-btn" type="primary" @click="onSubmit" :loading="loading">{{ $root.lang.forgotStep3.btn }}</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { checkPwd } from '@/utils/validate';
export default {
  data() {
    var checkPassword = (rule, value, callback) => {
      if (!value) {
        callback(new Error(this.$root.lang.forgotStep3.rule.passwordEmpty))
      } else if(!checkPwd(value)) {
        callback(new Error(this.$root.lang.forgotStep3.rule.passwordError))
      } else {
        callback()
      }
    }
    var checkConfirm = (rule, value, callback) => {
      if(!value) {
        callback(new Error(this.$root.lang.forgotStep3.rule.confirmEmpty))
      } else if(value != this.form.password) {
        callback(new Error(this.$root.lang.forgotStep3.rule.confirmError))
      } else {
        callback()
      }
    }
    return {
      loading: false,
      form: {
        username: '',
        password: '',
      },
      formRules: {
        password: [{ required: true, validator: checkPassword, trigger: 'blur' }],
        confirm: [{ required: true, validator: checkConfirm, trigger: 'blur' }]
      },
      errmsg: '',
      infoMsg: this.info
    }
  },
  props: {
    info: {
      type: String,
      default: ""
    }
  },
  methods: {
  	onSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.editPassword()
        } else {
          return false
        }
      })
  	},
    editPassword() {
      this.loading = true
      this.request({
        url: '/api/user/forget/password',
        method: 'post',
        data: {
          updateRecordNumber: this.infoMsg,
          password: this.form.password
        }
      }).then(response => {
        this.loading = false
        this.$emit('receive',{
          key: 'success',
          value: true
        })
      }).catch(error => {
        this.loading = false
        this.errmsg = error.errmsg
      })
    }
  },
  created() {
    console.log(this.infoMsg)
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
@import '@/css/style.scss';
.forgot-panel {
  position: relative;
  margin: 30px auto 0;
  padding: 100px 50px;
  width: 600px;
  background-color: #ffffff;
}
.forgot-form {
  margin: 0 auto;
  width: 60%;
}
.el-form-item {
  margin-bottom: 20px;
}
.forgot-btn {
  width: 100%;
}
</style>
